
make_texreg <- function(name, model, custom.model.names, custom.coef.map,
                        custom.note, resize_width = FALSE, caption, scalebox = 1, 
                        multiple.tasks = "clustered", refresh = T){
  addspacing <- function(table){
    x <- paste0("\\renewcommand{\\baselinestretch}{1.25}%\n", table)
    y <- paste0(x, "\n\\renewcommand{\\baselinestretch}{1.67}%")
    z <- str_replace(y, "\\n\\\\usepackage\\{threeparttable\\}\\n", "")
    return(z)
  }
  
  #path <- "../../Apps/Overleaf/BJPS R&R (Blind) - Justifying Distributions (Appendix)/tables/"
  
  if(multiple.tasks == "clustered"){
    custom.gof.names = c('R$^2$', 'Observations', 'Respondents')
    include.nclusts = TRUE
  }
  else if(multiple.tasks == "normal") {
    custom.gof.names = c('R$^2$', 'Observations')
    include.nclusts = FALSE
  } 
  else if(multiple.tasks == "fe") {
    custom.gof.names = c('Observations', "Respondents", "R$^2$ (full model)", "R$^2$ (proj model)")
    include.nclusts = NULL
  }

  table <- texreg(l = model,
                  custom.model.names = custom.model.names,
                  custom.coef.map = custom.coef.map,
                  custom.gof.names = custom.gof.names,
                  custom.note = custom.note,
                  caption = caption,
                  digits = 3,
                  include.ci = F,
                  caption.above = T,
                  scalebox = scalebox,
                  include.adjrs = F,
                  include.nclusts = include.nclusts,
                  include.rmse = F,
                  threeparttable = T,
                  label = paste0("tab:", name),
                  use.packages = F,
                  float.pos = "H",
                  booktabs = T)
  
  if(resize_width == TRUE){
  table <- str_replace(table, paste0("\\\\scalebox\\{",scalebox,"\\}\\{"), "\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{")
  }
  table <- addspacing(table)
  
  cat(table, sep = "\n")
  
  if(refresh == T){
    cat(table, sep = "\n", file = paste0(save_path, name, ".tex"))
  }
}
